/*
 * system.h
 *
 * define some system APIs
 *
 * Copyright (C) 2022 Microwell, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * Author: guoming<guoming@microwell.net>
 */

#ifndef __SYSTEM_H__
#define __SYSTEM_H__

#include "mcu_regs.h"
#include "config.h"
#include "types.h"
#include "drive.h"


/*=============================================================================
 *                               System config
 *===========================================================================*/
#define CONFIG_SCLK_SRC             (SYS_HIRC_FREQ)

#define CONFIG_SCLK_DIV             (CLK_DIV_1)

#define CONFIG_USING_WDOG           (1)
/* 
 * Watchdog timeout time:
 * 0, 8s    1, 4s    2, 2s    3, 1s 
 * 4, 500ms 5, 250ms 6, 125ms 7, 62.25ms
 */
#define CONFIG_WDOG_WDT             (1)
/* 1, wdog continue work at IDLE state; 0, do not work at IDLE state */
#define CONFIG_ENABLE_IDLE_DOG      (1)
/*=============================================================================
 *                             System config end
 *===========================================================================*/

#define SYS_SCLK_FREQ               (SYS_HIRC_FREQ / CONFIG_SCLK_DIV)

#define IRQ_ENABLE()                (irq_enable())
#define IRQ_DISABLE()               (irq_disable())

#define SYS_DEEP_ENABLE()           SET_BIT(CLKCON0, DSEN_BIT);   /// deep sleep enable

#define WDOG_ENABLE()               (WDTCON = ((CONFIG_ENABLE_IDLE_DOG << 7) | (CONFIG_WDOG_WDT << 4) | 0x0a))
#define WDOG_DISABLE()              (WDTCON = 0x05)
#define WDOG_KEEP_ALIVE()           (WDTCLR = 1)

#define DEBUG_PRINT_CH(ch)          (NOUSEREG = (ch))
void sys_init(void);
void sys_delay_cnt2us (uint8_t cnt);
void sys_delay_10us(uint16_t us10);
void sys_delay_ms(uint16_t ms);

uint8_t sys_get_version(void);

void irq_enable(void);
void irq_disable(void);
    
#endif /* __SYSTEM_H__ */

